<?php

namespace App\Http\Controllers\Api;

use App\Services\Agora\ChannelService;
use App\Services\Aliyun\Identify\IdCard;
use App\Services\Aliyun\Sms\AliYunSmsServices;
use App\Services\Configs\DefaultService;
use App\Services\Tencent\Live\StreamService;
use Illuminate\Http\Request;
//use App\Services\Huanxin\UserServices;
use App\Services\Netease\ChatServices as bbbb;
use App\Services\Netease\UserServices as aaaa;
use App\Services\Netease\GroupServices as cccc;
use App\Services\Huanxin\ChatServices;
use App\Services\Huanxin\GroupServices;
use App\Services\RongCloud\GroupService as RongCloud;
use App\Services\RongCloud\ChatService as RongCloudssss;
use App\Services\RongCloud\UserService as UserServicess;

use App\Services\Gaode\GaoDeService;

use App\Services\Aliyun\live\AuthKeyService;
use App\Services\Aliyun\live\StreamsService;

use App\Services\TencentIm\BaseConfig;
use App\Services\TencentIm\ChatServices as chats;
//use App\Services\TencentIm\GroupServices;
use App\Services\TencentIm\UserServices;

//use App\Services\Netease\UserServices;
//use App\Services\Netease\ChatServices;
//use App\Services\Netease\GroupServices;
use App\Services\Huanxin\ChatRoomServices;
use App\Services\Aliyun\Video\VideoServices;
use TencentCloud\Common\Credential;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Live\V20180801\LiveClient;
use TencentCloud\Live\V20180801\Models\DescribeLiveStreamPublishedListRequest;


class ChatController
{

    /**
     * 网易云信接口测试
     */
    public function chatTest(Request $request)
    {
         //腾讯云im
//                $aa = new UserServices();
//        $bb = $aa->accountImport('855','','');
//        var_dump($bb);die;


        //群组
              $aa = new GroupServices();
//        $bb = $aa->GroupsBan('178805187608577','86400000');
        $bb = $aa->GroupsDelBan('178805187608577');
//        $bb = $aa->updateGroup('3342343','牛逼哥','测试使用');
//        $bb = $aa->addGroupMember('888','88');
//        $bb = $aa->delGroupMember('3342343','14');
//        $bb = $aa->destroyGroupMember('3342343');
//        $bb = $aa->forbidSendMsg('47474','2');
//        $bb = $aa->forbidSendMsg('47474','2');
//        $aa = new UserServices();

//        $bb = $aa->friendAdd('88','855',"Tag_SNS_IM_AddSource");
//        $bb = $aa->delFriend('855','88');
//        $bb = $aa->blackListAdd('855','88');
//        $bb = $aa->delBlackList('855','88');

        var_dump($bb);die;

//        createGroup()

//     var_dump("3213123");die;

//                $data = new BaseConfig();
//        $aa = $data->getUserSig();
//        var_dump($aa);die;

//                $data = new chats();
//        $aa = $data->accountImport('14','','');
//        var_dump($aa);die;


//        $aa = new StreamsService();
//        $bb = $aa->pushUrl('tangfen','fen');//返回结果   rtmp://push.meiqiukang.com.cn/tangfen/fen?auth_key=1640830105-0-0-7d4b450c17f5d0427820916226d1a9a7
//        $bb = $aa->playUrl('tangfen','fen');
//        $bb = $aa->streamsOnlineList('play.meiqiukang.com.cn','1');
//        var_dump($bb);die;

//         $aa = new GaoDeService();
////        $aa =  $aa->getAddressLocation('广东省深圳市龙华区清湖地铁');
//        $aa =  $aa->beIpGetLocation('114.039215,22.670247');
//        var_dump($aa);die;


//        echo phpinfo();die;


//        $aa = new cccc();
//        $aa->createGroup('牛逼群s',90,['90','100'],'sdsad');
//        var_dump($aa);die;


//        $aa = new ChatServices();
//        $aa = new RongCloudssss();
//        $aa = new bbbb();
//        $aa = new aaaa();
//        $res = $aa->pushMessage('tangfen,81,82','推送测试','gdfgfdg');
//        $res = $aa->register('2','tangfen2');
//
//          var_dump($res);die;
        //文字
//        $msg = [
//            'msg'=>'fdgdfgfdg梵蒂冈地234324方官wewew',
//        ];
        //图片
//        $msg = [
//            'name'=>'群聊',
//            'url'=>'http://tangfen.oss-cn-hangzhou.aliyuncs.com/circle/20211207/2021120709315561aeb98bdf869.jpeg',
//            'md5'=>'sdsadsa34324d',
//            'ext'=>'jpeg',
//            'w'=>6814,
//            'h'=>2333,
//            'size'=>38824,
//        ];

//        $res = $aa->sendMessage('169','0','171','0',$msg);//单聊
//        $res = $aa->sendMessage('168','1','4079781603','0',$msg);//群聊
//        $res = $aa->sendMessage();
//        $res = $aa->querySessionMsg(100,90,1639969042000,1640228430000);
//        $res = $aa->queryGroupMsg(4062463329,90,1639969042000,1640228430000);//4062463329 网易云信群id
//        $res = $aa->createUser('aaaddd');



//        var_dump($res);die;
//




//        $data = null;
//        $cloud = new StreamService('tencent_live');
//        $data = $cloud->describeLiveStreamPublishedList("txpush.meiqiukang.com.cn", "2021-12-10T00:00:00Z", "2021-12-12T00:00:00Z");
//        $data = $cloud->describeLiveStreamState("txpush.meiqiukang.com.cn", "bf4d66a62fcf7a3afb826df552d859ad");
//        $data = $cloud->dropLiveStream("txpush.meiqiukang.com.cn", "bf4d66a62fcf7a3afb826df552d859ad");
//        $data = $cloud->forbidLiveStream("txpush.meiqiukang.com.cn", "bf4d66a62fcf7a3afb826df552d859ad",'2021-12-13T00:00:00Z','违规禁播');
//        $data = $cloud->resumeLiveStream("txpush.meiqiukang.com.cn", "bf4d66a62fcf7a3afb826df552d859ad");
//        $data = $cloud->describeLiveStreamOnlineList("txpush.meiqiukang.com.cn");
//        $data = $cloud->describeLiveStreamEventList(utc_time('2021-12-10 00:00:00'),utc_time('2021-12-15 00:00:00'));
//        $data = $cloud->describeLiveForbidStreamList();
//        return okBack($data);

//        $member['usernames'] = ['87','88'];
//        $member =['88'];
//        $member ='90,88';
//        $aa = $GroupServices->addChatGroupsMembers('166490503446530',$member,2);
//        $aa = $GroupServices->delChatGroupsMembers('166490503446530',$member,1);
//        $aa = $GroupServices->addGroupsBlacklists('166490503446530',$member,1);
//        $aa = $GroupServices->delGroupsBlacklists('166490503446530',$member,1);
//        $aa = $GroupServices->addGroupsMute('166490503446530',$member,-1);
//        $aa = $GroupServices->delGroupsMute('166490503446530',$member);

//        var_dump($aa);die;


        //用户信息 123456   254878  24781
//        $UserServices = new UserServices();
//        $aa =   $UserServices->createUser('343252343243432');//161616
//
//        var_dump($aa);die;

//        $aa =   $UserServices->updateUinfo('1','6s6ss11','牛ss逼哥666','','ssds34324s','2078801440@qq.com',2);
//        $aa =   $UserServices->getUinfos('6s6ss11',1);
//        $aa =   $UserServices->setDonnop('6s6ss11',3223,2);
//        $aa =   $UserServices->mute('6s6ss11','true',1);
//        $aa =   $UserServices->mute('6s6ss11','true',1);
//        $aa =   $UserServices->addFriend('6s6ss11','2266ss6','3','',2);
//        $aa =   $UserServices->addFriend('2266ss6','6s6ss11','3','',2);
//        $aa =   $UserServices->updateFriend('6s6ss11','66ss6','niubege','');
//        $aa =   $UserServices->deleteFriend('6s6ss11','66ss6','niubege','');
//        $aa =   $UserServices->getFriend('6s6ss11',1);
//        $aa =   $UserServices->specializeFriend('6s6ss11','2266ss6',1);
//        $aa =   $UserServices->listBlackFriend('6s6ss11',1);
//        $aa =   $UserServices->BlackUser('161616',1);
//        $aa =   $UserServices->unblockUser('161616',1);


//        $ChatServices = new ChatServices();
//          $body['msg'] = 'ceshissdsass';//文本
//        $aa = $ChatServices->sendMessage(1,'123456',0,'254878','0',$body);  //812907192388   812908412515  1637891901374   812918567503  1637893087085

//        $aa = $ChatServices->uploadMsg('gwettwgsgssgs323f','jpg',1);
//        $aa = $ChatServices->uploadMultiMsg('gwettwgsgssgs323f','',1);
//        $aa = $ChatServices->recallMsg('812908412515','7','123456','254878','1637891901374',2);
//        $aa = $ChatServices->recallMsg('812908412515','7','123456','254878','1637891901374',2);
//        $aa = $ChatServices->broadcastMsg('sdsadsdd',2);//需申请开通
//                $aa = $ChatServices->delMsgOneWayMsg('812918567503','13','123456','254878','1637893087085',1);
//                $aa = $ChatServices->delRoamSessionMsg('1','123456','254878',1);

//
//        $GroupServices = new GroupServices();
//        $member['members'] = toJson('254878');
//        $member =['254878'];
//        $aa = $GroupServices->createGroup(1,'牛逼哥群445','123456',$member,'测试群666');//   3940462361 3940454999  3940462366 3940455008
//        $aa = $GroupServices->addIntoGroup('3940462366','254878',['24781'],'','拉你进群',1);//3940420547  3940410192
//        $aa = $GroupServices->kickFromGroup('3940420547','123456','254878',1);//3940420547  3940410192
//        $aa = $GroupServices->removeGroup('3940410192','123456',1);//3940420547  3940410192
//        $aa = $GroupServices->updateGroup('3940462361','123456','修改群名称');//3940420547  3940410192
//        $aa = $GroupServices->queryGroup(['3940454999'],1,1);//3940420547  3940410192
//        $aa = $GroupServices->queryDetailGroup('3940462366',1);//3940420547  3940410192
//        $aa = $GroupServices->changeGroupOwner('3940462366','123456','254878',1);//3940420547  3940410192
//        $aa = $GroupServices->addGroupManager('3940462366','254878',['24781'],1);//3940420547  3940410192
//        $aa = $GroupServices->removeGroupManager('3940462366','254878',['24781'],1);//3940420547  3940410192
//        $aa = $GroupServices->joinTeams('254878',1);//3940420547  3940410192
//        $aa = $GroupServices->updateGroupNick('3940462366','254878','254878','昵称名称',1);//3940420547  3940410192
//        $aa = $GroupServices->muteTeamGroup('3940462366','254878',1,1);//3940420547  3940410192
//        $aa = $GroupServices->muteTlistGroup('3940462366','254878','254878',1,2);//3940420547  3940410192
//        $aa = $GroupServices->leaveGroup('3940462366','24781',1);//3940420547  3940410192
//        $aa = $GroupServices->muteTlistAllGroup('3940462366','254878',1,1);//3940420547  3940410192
//        $aa = $GroupServices->listTeamMuteGroup('3940462366','254878',1);//3940420547  3940410192

//        $ChatRoomServices = new ChatRoomServices();
//        $aa = $ChatRoomServices->createChatRoom('161616','测试聊天室1',2);
//        $aa = $ChatRoomServices->getChatRoom('161616',true,2);

//        $video = $request->file('video');
//        $VideoServices = new VideoServices();
//        $aa = $VideoServices->localUploadLocalVideo($video,"本田思域66" ,$description='',$coverURL='',$cateId='',$tags='',$templateGroupId='3bb7b379d1153a85a9eeba8af500fa99',$storageLocation='',$isCallback = 1);
//        $aa = $VideoServices->getPlayInfo('76e3f6ba035d488786cd37b70fe34782');
        //vi2L46Tw2a4UB4bAfpmVFVJK-1vmIXB

//        $aa = $VideoServices->getAIMediaAuditJobInfo('viUhJQdZ94045enjaUbVWG$-1vm@RF');//查询视频异步检测结果

        //提交智能视频/图片审核
//        $fileAIMediaAuditJob = new VideoServices();
//        $aa = $fileAIMediaAuditJob->submitTextAIMediaAuditJob("唐奋666",'232'); //$taskId智能检测任务ID(可获取智能检查结果）


//        $aa = $VideoServices->refreshUploadVideo('55c9a9c44aaa4896b0efc2366b63ac12');
//          $aa = '';

//        return okBack('网易云测试', $aa);

    }


    /**
     * 授权注册单个用户
     */
    public function createUser(Request $request)
    {
        $username = $request->username;
        $password = $request->password;
        $UserServices = new UserServices();
        $result = $UserServices->createUser($username, $password, 1);
        if ($result) {
            return okBack('环信注册成功', $result);
        } else {
            return errBack('环信注册失败');
        }
    }

    /**
     * 获取单个用户详细信息
     */
    public function getUser(Request $request)
    {
        $username = $request->username;
        $UserServices = new UserServices();
        $result = $UserServices->getUser($username, 1);
        if ($result) {
            return okBack('获取成功', $result);
        } else {
            return errBack('获取失败');
        }
    }

    /**
     * 删除单个用户
     */
    public function deleteUser(Request $request)
    {
        $username = $request->username;
        $UserServices = new UserServices();
        $result = $UserServices->deleteUser($username, 1);
        if ($result) {
            return okBack('删除成功', $result);
        } else {
            return errBack('删除失败');
        }
    }

    /**
     * 修改用户密码
     */
    public function resetPassword(Request $request)
    {
        $username = $request->username;
        $newpassword = $request->newpassword;
        $UserServices = new UserServices();
        $result = $UserServices->resetPassword($username, $newpassword, 1);
        if ($result) {
            return okBack('修改成功', $result);
        } else {
            return errBack('修改失败');
        }
    }

    /*
    修改用户昵称
   */
    public function editNickname(Request $request)
    {
        $username = $request->username;
        $nickname = $request->nickname;
        $UserServices = new UserServices();
        $result = $UserServices->editNickname($username, $nickname, 1);
        if ($result) {
            return okBack('修改成功', $result);
        } else {
            return errBack('修改失败');
        }
    }

    /*
    设置推送消息展示方式
    */
    public function editPushType(Request $request)
    {
        $notification_display_style = $request->notification_display_style;
        $username = $request->username;
        $UserServices = new UserServices();
        $result = $UserServices->editNickname($username, $notification_display_style, 1);
        if ($result) {
            return okBack('修改成功', $result);
        } else {
            return errBack('修改失败');
        }
    }

    /*
    设置免打扰
    */
    public function setNotDisturb(Request $request)
    {
        $username = $request->username;
        $notification_no_disturbing = $request->notification_no_disturbing;
        $notification_no_disturbing_start = $request->notification_no_disturbing_start;
        $notification_no_disturbing_end = $request->notification_no_disturbing_end;
        $UserServices = new UserServices();
        $result = $UserServices->editNickname($username, $notification_no_disturbing, $notification_no_disturbing_start, $notification_no_disturbing_end, 1);
        if ($result) {
            return okBack('修改成功', $result);
        } else {
            return errBack('修改失败');
        }
    }

    /*
        添加好友
    */
    public function addFriend(Request $request)
    {
        $username = $request->username;
        $friend_name = $request->friend_name;
        $UserServices = new UserServices();
        $result = $UserServices->addFriend($username, $friend_name, 1);
        if ($result) {
            return okBack('修改成功', $result);
        } else {
            return errBack('修改失败');
        }
    }

    /*
        删除好友
    */
    public function deleteFriend(Request $request)
    {
        $username = $request->username;
        $friend_name = $request->friend_name;
        $UserServices = new UserServices();
        $result = $UserServices->deleteFriend($username, $friend_name, 1);
        if ($result) {
            return okBack('修改成功', $result);
        } else {
            return errBack('修改失败');
        }
    }

    /*
        查看好友列表
    */
    public function showFriends(Request $request)
    {
        $username = $request->username;
        $UserServices = new UserServices();
        $result = $UserServices->showFriends($username, 1);
        if ($result) {
            return okBack('修改成功2', $result);
        } else {
            return errBack('修改失败');
        }
    }

    /*
        查看用户黑名单
    */
    public function getBlacklist(Request $request)
    {
        $username = $request->username;
        $UserServices = new UserServices();
        $result = $UserServices->getBlacklist($username, 1);
        if ($result) {
            return okBack('修改成功s2', $result);
        } else {
            return errBack('修改失败');
        }
    }

    /*
    加入黑名单
    */
    public function addUserForBlacklist(Request $request)
    {
        $username = $request->username;
        $UserServices = new UserServices();
        $result = $UserServices->addUserForBlacklist($username, 1);
        if ($result) {
            return okBack('修改成功11', $result);
        } else {
            return errBack('修改失败');
        }
    }


    /*
        剔除黑名单
    */
    public function deleteUserFromBlacklist(Request $request)
    {
        $username = $request->username;
        $blocked_name = $request->blocked_name;
        $UserServices = new UserServices();
        $result = $UserServices->deleteUserFromBlacklist($username, $blocked_name, 1);
        if ($result) {
            return okBack('修改成功11', $result);
        } else {
            return errBack('修改失败');
        }
    }

    /*
       查看用户是否在线
    */
    public function isOnline(Request $request)
    {
        $username = $request->username;
        $UserServices = new UserServices();
        $result = $UserServices->isOnline($username, 1);
        if ($result) {
            return okBack('修改成功1s1', $result);
        } else {
            return errBack('修改失败');
        }
    }

    /*
    查看用户离线消息数
*/
    public function getOfflineMessages(Request $request)
    {
        $username = $request->username;
        $UserServices = new UserServices();
        $result = $UserServices->getOfflineMessages($username, 1);
        if ($result) {
            return okBack('修改成功1aa', $result);
        } else {
            return errBack('修改失败');
        }
    }

    /*
    查看某条消息的离线状态
    ----deliverd 表示此用户的该条离线消息已经收到
*/
    public function getOfflineMessageStatus(Request $request)
    {
        $username = $request->username;
        $msg_id = $request->msg_id;
        $UserServices = new UserServices();
        $result = $UserServices->getOfflineMessageStatus($username, $msg_id, 1);
        if ($result) {
            return okBack('修改成功1qqaa', $result);
        } else {
            return errBack('修改失败');
        }
    }

    /*
        禁用用户账号
    */
    public function deactiveUser(Request $request)
    {
        $username = $request->username;
        $UserServices = new UserServices();
        $result = $UserServices->deactiveUser($username, 1);
        if ($result) {
            return okBack('修改成功w', $result);
        } else {
            return errBack('修改失败');
        }
    }

    /*
        解禁用户账号
    */
    public function activeUser(Request $request)
    {
        $username = $request->username;
        $url = config('api.chat.url') . 'users/' . $username . '/activate';
        $header = array($this->getToken());
        $result = postCurl($url, '', $header);
        return okBack('0', $result);
    }

    /*
        强制用户下线
    */
    public function disconnectUser(Request $request)
    {
        $username = $request->username;
        $url = config('api.chat.url') . 'users/' . $username . '/disconnect';
        $header = array($this->getToken());
        $result = postCurl($url, '', $header, 'GET');
        return okBack('0', $result);
    }

    /*
    设置用户属性
    */
    public function setUserProperties(Request $request)
    {
        $username = $request->username;
        $nickname = $request->nickname;
        $avatarurl = $request->avatarurl;
        $phone = $request->phone;
        $gender = $request->gender;
        $UserServices = new UserServices();
        $result = $UserServices->setUserProperties($username, $nickname, $avatarurl, $phone, $gender, 2);
        if ($result) {
            return okBack('修改成功c', $result);
        } else {
            return errBack('修改失败');
        }
    }

    /*
      获取用户属性
    */
    public function getUserProperties(Request $request)
    {
        $username = $request->username;
        $url = config('api.chat.url') . 'metadata/users/' . $username;
        $header = array($this->getToken());
        $result = postCurl($url, '', $header, 'GET');
        return okBack('0', $result);
    }

    /*
    删除用户属性
    */
    public function delUserProperties(Request $request)
    {
        $username = $request->username;
        $url = config('api.chat.url') . 'metadata/users/' . $username;
        $header = array($this->getToken());
        $result = postCurl($url, '', $header, 'DELETE');
        return okBack('0', $result);
    }

    /*
    发送文本消息
   */
    public function sendText(Request $request)
    {
        $target_type = $request->target_type;
        $target = $request->target;
        $contents = $request->contents;
        $ext = $request->ext;
        $ChatServices = new ChatServices();
        $result = $ChatServices->sendText($target_type, $target, $contents, 'admin', 2);
        if ($result) {
            return okBack('修改成功s7', $result);
        } else {
            return errBack('修改失败');
        }
    }


    /*
        发图片消息
    */
    public function sendImg(Request $request)
    {
        $fileInfo = saveFile($request->filePath);

        $from = $request->from;
        $target_type = $request->target_type;
        $target = $request->target;
        $ChatServices = new ChatServices();
        $result = $ChatServices->sendImg($target_type, $target, $request->filePath, 'admin', 1);
        if ($result) {
            return okBack('修改成功44s', $result);
        } else {
            return errBack('修改失败');
        }
    }

    /*
        上传图片或文件
    */
    public function uploadFile($filePath)
    {
        $url = config('api.chat.url') . 'chatfiles';
        $file = file_get_contents($filePath);
        $body['file'] = $file;
        $header = array('Content-type: multipart/form-data', $this->getToken(), "restrict-access:true");
        $result = postCurl($url, $body, $header, 'XXX');
        return $result;

    }

    /*
    发送语音消息
    */
    public function sendVoice(Request $request)
    {
        $filePath = $request->filePath;
        $from = $request->from;
        $target_type = $request->target_type;
        $target = $request->target;
        $filename = $request->filename;
        $length = $request->length;

        $ChatServices = new ChatServices();
        $result = $ChatServices->sendVoice($target_type, $target, $filePath, 'admin', $filename, 1);
        if ($result) {
            return okBack('修改成功88', $result);
        } else {
            return errBack('修改失败');
        }

    }

    /*
   发送视频消息
   */
    public function sendVideo(Request $request)
    {
        $filePath = $request->filePath;
        $from = $request->from;
        $target_type = $request->target_type;
        $target = $request->target;
        $filename = $request->filename;
        $length = $request->length;
        $thumb = $request->thumb;
        $thumb_secret = $request->thumb_secret;
        $ChatServices = new ChatServices();
        $result = $ChatServices->sendVoice($target_type, $target, $filePath, 'admin', $filename, 1);
        if ($result) {
            return okBack('修改成功3333', $result);
        } else {
            return errBack('修改失败');
        }

    }

    /*
   发送位置消息
   */
    public function sendPosition(Request $request)
    {
        $from = $request->from;
        $target_type = $request->target_type;
        $target = $request->target;

        $ChatServices = new ChatServices();
        $result = $ChatServices->sendPosition($target_type, $target, 'admin', 1);
        if ($result) {
            return okBack('修改成功322333', $result);
        } else {
            return errBack('修改失败');
        }
    }


    //群组管理
    /*
   获取App中所有的群组
   */
    public function getAllGroup(Request $request)
    {
        $limit = $request->limit;
        $cursor = $request->cursor;

        $GroupServices = new GroupServices();
        $result = $GroupServices->getAllGroup(15, 1, 1);
        if ($result) {
            return okBack('修改成功V', $result);
        } else {
            return errBack('修改失败');
        }
    }

    /*
获取一个用户参与的所有群组
*/
    public function getJoinedChatgroups(Request $request)
    {
        $username = $request->username;
        $url = config('api.chat.url') . 'users/' . $username . '/joined_chatgroups';
        $header = array($this->getToken());
        $result = postCurl($url, '', $header, 'GET');
        return okBack('0', $result);
    }

    /*
获取群组详情
*/
    public function getGroupDetails(Request $request)
    {
        $group_ids = $request->group_ids;
//        $g_ids=implode(',',$group_ids);
        $url = config('api.chat.url') . 'chatgroups/' . $group_ids;
        $header = array($this->getToken());
        $result = postCurl($url, '', $header, 'GET');
        return okBack('0', $result);
    }

    //创建一个群组
    public function createGroup(Request $request)
    {
        $param = $request->all();
//        $options = [
//            'groupname' => $param['groupname'],
//            'desc' => $param['desc'],
//            'public' => $param['public'],
//            'maxusers' => $param['maxusers'],
//            'allowinvites' => $param['allowinvites'],
//            'owner' => $param['owner'],
//            'members_only' => $param['members_only'],
//            'members' => ['tangfen7']
//        ];
        $GroupServices = new GroupServices();
        $result = $GroupServices->createGroup($param['groupname'], $param['desc'], $param['allowinvites'], $param['public'], $param['owner'], 1);
        if ($result) {
            return okBack('修改成功p', $result);
        } else {
            return errBack('修改失败');
        }
    }

    //修改群组信息
    public function updateGroup(Request $request)
    {
        $param = $request->all();
//        $options = [
//            'groupname' => $param['groupname'],
//            'desc' => $param['desc'],
//            'public' => $param['public'],
//            'maxusers' => $param['maxusers'],
//            'allowinvites' => $param['allowinvites'],
//            'owner' => $param['owner'],
//            'members_only' => $param['members_only'],
//        ];
        $GroupServices = new GroupServices();
        $result = $GroupServices->updateGroup($param['groupname'], $param['desc'], $param['public'], $param['allowinvites'], $param['owner'], $param['group_id'], 1);
        if ($result) {
            return okBack('修改成功ww33', $result);
        } else {
            return errBack('修改失败');
        }
    }

    //删除群组
    public function deleteGroup(Request $request)
    {
        $group_id = $request->group_id;
        $GroupServices = new GroupServices();
        $result = $GroupServices->deleteGroup($group_id, 1);
        if ($result) {
            return okBack('修改成功ww33', $result);
        } else {
            return errBack('修改失败');
        }
    }

//管理群组成员
//分页获取群组成员
    public function getChatGroups(Request $request)
    {
        $group_id = $request->group_id;
        $GroupServices = new GroupServices();
        $result = $GroupServices->deleteGroup($group_id, 1);
        if ($result) {
            return okBack('修改成功wwww33', $result);
        } else {
            return errBack('修改失败');
        }
    }

//添加单个群组成员
    public function addChatGroupsMember(Request $request)
    {
        $group_id = $request->group_id;
        $username = $request->username;
        $GroupServices = new GroupServices();
        $result = $GroupServices->addChatGroupsMember($group_id, $username, 1);
        if ($result) {
            return okBack('修改成功wwww33', $result);
        } else {
            return errBack('修改失败');
        }
    }

    //移除单个群组成员
    public function delChatGroupsMember(Request $request)
    {
        $group_id = $request->group_id;
        $username = $request->username;
        $url = config('api.chat.url') . 'chatgroups/' . $group_id . '/users/' . $username;
        $header = array($this->getToken());
        $result = postCurl($url, '', $header, 'DELETE');
        return okBack('0', $result);
    }

    //获取群管理员列表
    public function getChatGroupsMemberAdmin(Request $request)
    {
        $group_id = $request->group_id;
        $url = config('api.chat.url') . 'chatgroups/' . $group_id . '/admin/';
        $header = array($this->getToken());
        $result = postCurl($url, '', $header, 'GET');
        return okBack('0', $result);
    }

    //添加群管理员
    public function addChatGroupsAdmin(Request $request)
    {
        $group_id = $request->group_id;
        $username = $request->username;
        $url = config('api.chat.url') . 'chatgroups/' . $group_id . '/admin/';
        $header = array($this->getToken());
        $body = [
            'newadmin' => $username
        ];
        $body = json_encode($body);
        $result = postCurl($url, $body, $header, 'POST');
        return okBack('0', $result);
    }

    //移除群管理员
    public function delChatGroupsAdmin(Request $request)
    {
        $group_id = $request->group_id;
        $oldadmin = $request->oldadmin;
        $url = config('api.chat.url') . 'chatgroups/' . $group_id . '/admin/' . $oldadmin;
        $header = array($this->getToken());
        $result = postCurl($url, '', $header, 'DELETE');
        return okBack('0', $result);
    }

    //转让群组
    public function transferChatGroupsAdmin(Request $request)
    {
        $group_id = $request->group_id;
        $newowner = $request->newowner;
        $url = config('api.chat.url') . 'chatgroups/' . $group_id;
        $header = array($this->getToken());
        $options = [
            'newowner' => $newowner
        ];
        $body = json_encode($options);
        $result = postCurl($url, $body, $header, 'PUT');
        return okBack('0', $result);
    }


    //群组管理黑名单
    //查询群组黑名单
    public function getGroupsBlacklist(Request $request)
    {
        $group_id = $request->group_id;
        $url = config('api.chat.url') . 'chatgroups/' . $group_id . '/blocks/users';
        $header = array($this->getToken());
        $result = postCurl($url, '', $header, 'GET');
        return okBack('0', $result);
    }

//添加单个用户至群组黑名单
    public function addGroupsBlacklist(Request $request)
    {
        $group_id = $request->group_id;
        $username = $request->username;
        $url = config('api.chat.url') . 'chatgroups/' . $group_id . '/blocks/users/' . $username;
        $header = array($this->getToken());
        $result = postCurl($url, '', $header, 'POST');
        return okBack('0', $result);
    }

    //从群组黑名单移除单个用户
    public function delGroupsBlacklist(Request $request)
    {
        $group_id = $request->group_id;
        $username = $request->username;
        $url = config('api.chat.url') . 'chatgroups/' . $group_id . '/blocks/users/' . $username;
        $header = array($this->getToken());
        $result = postCurl($url, '', $header, 'DELETE');
        return okBack('0', $result);
    }


    //管理禁言
    //添加禁言
    public function addGroupsMute(Request $request)
    {
        $mute_duration = $request->mute_duration;
        $group_id = $request->group_id;
        $username = $request->username;
        $url = config('api.chat.url') . 'chatgroups/' . $group_id . '/mute';
        $header = array($this->getToken());
        $options = [
            'mute_duration' => $mute_duration,
            'usernames' => [$username]
        ];
        $body = json_encode($options, true);
        $result = postCurl($url, $body, $header, 'POST');
        return okBack('0', $result);
    }

    //移除禁言
    public function delGroupsMute(Request $request)
    {
        $group_id = $request->group_id;
        $username = $request->username;
        $url = config('api.chat.url') . 'chatgroups/' . $group_id . '/mute/' . $username;
        $header = array($this->getToken());
        $result = postCurl($url, '', $header, 'DELETE');
        return okBack('0', $result);
    }

    //获取禁言列表
    public function getdelGroupsMute(Request $request)
    {
        $group_id = $request->group_id;
        $url = config('api.chat.url') . 'chatgroups/' . $group_id . '/mute';
        $header = array($this->getToken());
        $result = postCurl($url, '', $header, 'GET');
        return okBack('0', $result);
    }

    //聊天室管理
//获取所有的聊天室
    public function getAllChatRooms(Request $request)
    {
        $url = config('api.chat.url') . 'chatrooms';
        $header = array($this->getToken());
        $result = postCurl($url, '', $header, 'GET');
        return okBack('0', $result);
    }

//获取用户加入的聊天室
    public function joinedChatRooms(Request $request)
    {
        $username = $request->username;
        $url = config('api.chat.url') . 'users/' . $username . '/joined_chatrooms';
        $header = array($this->getToken());
        $result = postCurl($url, '', $header, 'GET');
        return okBack('0', $result);
    }

    //获取聊天室详情
    public function getChatRoomsdetails(Request $request)
    {
        $chatroom_id = $request->chatroom_id;
        $url = config('api.chat.url') . 'chatrooms/' . $chatroom_id;
        $header = array($this->getToken());
        $result = postCurl($url, '', $header, 'GET');
        return okBack('0', $result);
    }

    //创建一个聊天室
    public function createChatRooms(Request $request)
    {
        $param = $request->all();
        $url = config('api.chat.url') . 'chatrooms';
        $options = [
            'name' => $param['name'],
            'description' => $param['description'],
            'maxusers' => $param['maxusers'],
            'owner' => $param['owner'],
            'name' => [$param['members']],
        ];
        $header = array($this->getToken());
        $body = json_encode($options);
        $result = postCurl($url, $body, $header, 'GET');
        return okBack('0', $result);
    }

    //修改聊天室信息
    public function uptadeChatRooms(Request $request)
    {
        $param = $request->all();
        $url = config('api.chat.url') . 'chatrooms/' . $param['chatroom_id'];
        $options = [
            'name' => $param['name'],
            'description' => $param['description'],
            'maxusers' => $param['maxusers'],
        ];
        $header = array($this->getToken());
        $body = json_encode($options);
        $result = postCurl($url, $body, $header, 'PUT');
        return okBack('0', $result);
    }

    //删除聊天室
    public function delChatRooms(Request $request)
    {
        $param = $request->all();
        $url = config('api.chat.url') . 'chatrooms/' . $param['chatroom_id'];
        $header = array($this->getToken());
        $result = postCurl($url, '', $header, 'DELETE');
        return okBack('0', $result);
    }

    //管理聊天室成员
    //分页获取聊天室成员
    public function getChatRoomsUser(Request $request)
    {
        $chatroom_id = $request->chatroom_id;
        $url = config('api.chat.url') . 'chatrooms/' . $chatroom_id . '/users';
        $header = array($this->getToken());
        $result = postCurl($url, '', $header, 'GET');
        return okBack('0', $result);
    }

    //添加单个聊天室成员
    public function addChatRoomsUser(Request $request)
    {
        $chatroom_id = $request->chatroom_id;
        $username = $request->username;
        $url = config('api.chat.url') . 'chatrooms/' . $chatroom_id . '/users/' . $username;
        $header = array($this->getToken());
        $result = postCurl($url, '', $header, 'POST');
        return okBack('0', $result);
    }

    //删除单个聊天室成员
    public function delChatRoomsUser(Request $request)
    {
        $chatroom_id = $request->chatroom_id;
        $username = $request->username;
        $url = config('api.chat.url') . 'chatrooms/' . $chatroom_id . '/users/' . $username;
        $header = array($this->getToken());
        $result = postCurl($url, '', $header, 'DELETE');
        return okBack('0', $result);
    }

    //获取聊天室管理员列表
    public function getChatRoomsAdmin(Request $request)
    {
        $chatroom_id = $request->chatroom_id;
        $url = config('api.chat.url') . 'chatrooms/' . $chatroom_id . '/admin';
        $header = array($this->getToken());
        $result = postCurl($url, '', $header, 'GET');
        return okBack('0', $result);
    }

    //添加聊天室管理员
    public function addChatRoomsAdmin(Request $request)
    {
        $chatroom_id = $request->chatroom_id;
        $username = $request->username;
        $url = config('api.chat.url') . 'chatrooms/' . $chatroom_id . '/admin';
        $options = [
            'newadmin' => $username
        ];
        $header = array($this->getToken());
        $body = json_encode($options);
        $result = postCurl($url, $body, $header, 'POST');
        return okBack('0', $result);
    }

    //移除聊天室管理员
    public function delChatRoomsAdmin(Request $request)
    {
        $chatroom_id = $request->chatroom_id;
        $oldadmin = $request->oldadmin;
        $url = config('api.chat.url') . 'chatrooms/' . $chatroom_id . '/admin/' . $oldadmin;
        $header = array($this->getToken());
        $result = postCurl($url, '', $header, 'DELETE');
        return okBack('0', $result);
    }

    //管理禁言
    //获取禁言列表
    public function getChatRoomsMute(Request $request)
    {
        $chatroom_id = $request->chatroom_id;
        $url = config('api.chat.url') . 'chatrooms/' . $chatroom_id . '/mute';
        $header = array($this->getToken());
        $result = postCurl($url, '', $header, 'GET');
        return okBack('0', $result);
    }

    //添加禁言
    public function addChatRoomsMute(Request $request)
    {
        $username = $request->username;
        $mute_duration = $request->mute_duration;
        $chatroom_id = $request->chatroom_id;
        $url = config('api.chat.url') . 'chatrooms/' . $chatroom_id . '/mute';
        $header = array($this->getToken());
        $options = [
            'mute_duration' => $mute_duration,
            'usernames' => [$username],
        ];
        $body = json_encode($options);
        $result = postCurl($url, $body, $header, 'POST');
        return okBack('0', $result);
    }

    //移除禁言
    public function delChatRoomsMute(Request $request)
    {
        $username = $request->username;
        $chatroom_id = $request->chatroom_id;
        $url = config('api.chat.url') . 'chatrooms/' . $chatroom_id . '/mute/' . $username;
        $header = array($this->getToken());
        $result = postCurl($url, '', $header, 'DELETE');
        return okBack('0', $result);
    }

    //管理超级管理员
    //获取超级管理员列表
    public function getChatRoomsSuperAdmin(Request $request)
    {
        $url = config('api.chat.url') . 'chatrooms/super_admin';
        $header = array($this->getToken());
        $result = postCurl($url, '', $header, 'GET');
        return okBack('0', $result);
    }


    //添加超级管理员
    public function addChatRoomsSuperAdmin(Request $request)
    {
        $username = $request->username;
        $url = config('api.chat.url') . 'chatrooms/super_admin';
        $options = [
            'superadmin' => $username,
        ];
        $header = array($this->getToken());
        $body = json_encode($options);
        $result = postCurl($url, $body, $header, 'POST');
        return okBack('0', $result);
    }


    //移除超级管理员
    public function delChatRoomsSuperAdmin(Request $request)
    {
        $username = $request->username;
        $url = config('api.chat.url') . 'chatrooms/super_admin/' . $username;
        $header = array($this->getToken());
        $result = postCurl($url, '', $header, 'DELETE');
        return okBack('0', $result);
    }


}
